<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Study extends CI_Controller {
	var $title; 
	var $salt;
	var $member_type=false;
	var $member_data=array();
	var $member=array();
	
	public function __construct()
	{
		parent::__construct();
			
		$this->member[0]='student';
		$this->member[1]='teacher';
		$this->member[2]='educator';
		$this->member[3]='parent';
		$this->member[4]='institute';
		
		#load all database and required models
		$this->load->database();
		$this->load->model('common_model');
		$this->load->model('study_model');	

		#check authentication
		if ($this->common_model->is_user_logged_in()) 
		{
		   $this->data['member_data']=$this->session->all_userdata();
		}
		
		#common setting 
		$config['salt']=$this->common_model->salt();
		$config['separator']=$this->common_model->title_separator();
		$config['title']=$this->common_model->base_title();
		$config['short_title']=$this->common_model->short_title();
	    $this->data['config_data']=$config;
		$this->title=$this->common_model->base_title().' '.$this->common_model->title_separator().' ';
		$this->salt=$this->common_model->salt();
		#######
		$this->data['type']=$this->member;
	    $this->data['javscript_error']=$this->lang->line('javscript_error');
		$this->data['label']=$this->lang->line('php_label');
		$this->data['page']=$this->lang->line('page');
		$this->data['static_info']=$this->lang->line('static_info');
		$this->data['gender']=$this->lang->line('gender');
		$this->data['header']=$this->lang->line('header');
		$this->data['footer']=$this->lang->line('footer');
		$this->data['static_text']=$this->lang->line('static_text');
	}
	
	public function index()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$this->data['class_list'] =$this->common_model->class_list();
		$this->load->view('study/study1', $this->data);
	}
	
	public function getEmail()
	{
		$this->load->helper('cookie');
		
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$this->data['page_type'] = 'email';
		$e_cookie = 'grc_'.base64_encode('grc_first_email');
		
		if ($this->common_model->is_user_logged_in()) 
		{
			$this->form_validation->set_rules('subject',$this->data['label']['subject'],'trim|required|xss_clean');
			$this->form_validation->set_rules('class',$this->data['label']['class'],'trim|required|xss_clean|integer');
			$this->form_validation->set_rules('book',$this->data['label']['book'],'trim|required|xss_clean');
			$this->form_validation->set_rules('chapter',$this->data['label']['chapter'],'trim|required|xss_clean');
				
			if ($this->form_validation->run())
			{
	            $this->session->set_userdata('study_data', $this->security->xss_clean($this->input->post()));
				$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
				
				$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
				$question_detail = $this->study_model->getChapterDetail($study_data);
				redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($question_detail[0]['chapter_title'])))."&q_type=".trim($question_detail[0]['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail[0]['question_topic'])))."&question_id=".trim($question_detail[0]['question_mcq_seq_id']));
	            exit();
			}
			else {
				//$this->data['error'] = validation_errors();
				$this->data['error'] = '* '.$this->data['javscript_error']['invalid_input'];
				$this->data['title'] =$this->title.$this->data['page']['study']['title'];
				$this->data['class_list'] =$this->common_model->class_list();
				$this->data['inputs'] = $this->input->post();
				$this->load->view('study/study1', $this->data);
			}
		}
		//if email cookie exists
		else if($this->input->cookie($e_cookie, TRUE))
		{
			$email = base64_decode(htmlspecialchars_decode($this->security->xss_clean($this->input->cookie($e_cookie, TRUE))));
			$this->data['mail_status'] = $this->common_model->getUserData($email);
			$activation = $this->data['mail_status'][0]['activation'];
			if(isset($this->data['mail_status'][0]['active']) && $this->data['mail_status'][0]['active']=='N')
			{
				$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
				$this->common_model->email_send($email, $mail_msg);
				$this->session->set_userdata('change_email',$email);
			}
							
			$this->session->set_userdata('study_data', $this->security->xss_clean($this->input->post()));
			$mail = base64_decode($this->security->xss_clean($this->input->cookie($e_cookie, TRUE)));
			$this->data['user_detail'] = $this->data['mail_status'];
			$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->data['user_detail']));
			redirect(BASE_URL.'study/get_name');
		}
		//if email entered
		else if($this->input->post('email_enter') == 'email')
		{
			$email = $this->security->xss_clean($this->input->post('email1'));
			
			if($this->input->post('category') == 'now')
			{
				$this->form_validation->set_rules('email1',$this->data['label']['email'],'trim|required|valid_email|xss_clean');
				if ($this->form_validation->run())
				{
					if(!$this->common_model->verify_email($email))
					{
						$this->data['new_user_id'] = $this->study_model->insertUserVisitData(array('email'=>$email));
						if($this->data['new_user_id'])
						{
							// set cookie 
							$cookie = array(
									'name'   => base64_encode('grc_first_email'),
									'value'  => base64_encode(htmlspecialchars($email, ENT_QUOTES)),
									'expire' => 86400 * 7,
									//'domain' => '.'.$_SERVER['SERVER_NAME'],
									'domain' => $_SERVER['SERVER_NAME'],
									'path'   => '/',
									'prefix' => 'grc_',
									);
							$this->input->set_cookie($cookie);
							
							$this->data['mail_status'] = $this->common_model->getUserData($email);
							$activation = $this->data['mail_status'][0]['activation']; 
							$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
							$this->common_model->email_send($email, $mail_msg);
							
							$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
							
							$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
							$question_detail = $this->study_model->getChapterDetail($study_data);
							redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($question_detail[0]['chapter_title'])))."&q_type=".trim($question_detail[0]['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail[0]['question_topic'])))."&question_id=".trim($question_detail[0]['question_mcq_seq_id']));
						}
						else {
							$this->data['error'] = 'There was some problem in registering your email. Please try again later';
							$this->load->view('study/study_info_collect', $this->data);
						}
					}
					else {
						$this->data['mail_status'] = $this->common_model->getUserData($email);
						$activation = $this->data['mail_status'][0]['activation']; 
						if(isset($this->data['mail_status'][0]['active']) && $this->data['mail_status'][0]['active']=='N')
						{
							$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
							$this->common_model->email_send($email, $mail_msg);
						}
						
						$this->data['user_detail'] = $this->data['mail_status'];
						$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->data['user_detail']));
						redirect(BASE_URL.'study/get_name');
					}
				}
				else {
					$this->data['error'] = validation_errors();
					$this->load->view('study/study_info_collect', $this->data);
				}
			}
			else if($this->input->post('category') == 'later')
			{
				$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
				$question_detail = $this->study_model->getChapterDetail($study_data);
				redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($question_detail[0]['chapter_title'])))."&q_type=".trim($question_detail[0]['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail[0]['question_topic'])))."&question_id=".trim($question_detail[0]['question_mcq_seq_id']));
			}
		}
		//if email cookie not exists 
		else {
			if($this->input->post())
			{
				$this->form_validation->set_rules('subject',$this->data['label']['subject'],'trim|required|xss_clean');
				$this->form_validation->set_rules('class',$this->data['label']['class'],'trim|required|xss_clean|integer');
				$this->form_validation->set_rules('book',$this->data['label']['book'],'trim|required|xss_clean');
				$this->form_validation->set_rules('chapter',$this->data['label']['chapter'],'trim|required|xss_clean');
				
				if ($this->form_validation->run())
				{
					$this->session->set_userdata('study_data', $this->security->xss_clean($this->input->post()));
					$this->load->view('study/study_info_collect', $this->data);
				}
				else {
					//$this->data['error'] = validation_errors();
					$this->data['error'] = '* '.$this->data['javscript_error']['invalid_input'];
					$this->data['title'] =$this->title.$this->data['page']['study']['title'];
					$this->data['class_list'] =$this->common_model->class_list();
					$this->data['inputs'] = $this->input->post();
					$this->load->view('study/study1', $this->data);
				}
			}
			else {
				redirect(BASE_URL.'study');
			}
		}
	}

	public function getName()
	{
		$this->load->helper('cookie');
		
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$this->data['page_type'] = 'name';
		$n_cookie = 'grc_347786263';
		
		//if email cookie exists
		if($this->input->cookie($n_cookie, TRUE))
		{
			$c_name_id = base64_decode($this->security->xss_clean($this->input->cookie($n_cookie, TRUE)));
			
			$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($c_name_id));
						
			$this->data['user_detail'] = $this->data['mail_status'];
			$this->session->set_userdata('study_name_data', $this->security->xss_clean($this->data['user_detail']));
			redirect(BASE_URL.'study/get_school');
		}
		//if email entered
		else if($this->input->post('name_enter') == 'name')
		{
			$fname = $this->security->xss_clean($this->input->post('first_name1'));
			$lname = $this->security->xss_clean($this->input->post('last_name1'));
			
			if($this->input->post('category') == 'now')
			{
				if($this->input->post('email2') && $this->input->post('email2') != '')
				{
					$this->form_validation->set_rules('email2',$this->data['label']['email'],'trim|required|valid_email|xss_clean');
				}
				$this->form_validation->set_rules('first_name1',$this->data['label']['first_name'],'trim|required|xss_clean|alpha_dash');
				$this->form_validation->set_rules('last_name1',$this->data['label']['last_name'],'trim|required|xss_clean|alpha_dash');
				
				if ($this->form_validation->run())
				{
					if($this->session->userdata('study_email_data'))
					{
						$value = $this->session->userdata('study_email_data');
						$this->session->unset_userdata('study_email_data');	
					}
					else if($this->session->userdata('study_change_email_data'))
					{
						$value = $this->session->userdata('study_change_email_data');
						$this->session->unset_userdata('study_change_email_data');
					}
					
					//--- if updating email------
					if($this->input->post('email2') && $this->input->post('email2') != '')
					{
						$email = $this->security->xss_clean($this->input->post('email2'));
						if(!$this->common_model->verify_email($email))
						{
							$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($value[0]['member_id']));
							$activation = $this->data['mail_status'][0]['activation']; 
								
							$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
							$this->common_model->email_send($email, $mail_msg);
								
							$this->study_model->updateUserVisitData(array('email'=>$email,'first_name'=>$fname, 'last_name'=>$lname), $this->security->xss_clean($value[0]['member_id']));
							// set cookie 
							$cookie = array(
										'name'   => '347786263',
										'value'  => base64_encode(htmlspecialchars($value[0]['member_id'], ENT_QUOTES)),
										'expire' => 86400 * 7,
										//'domain' => '.'.$_SERVER['SERVER_NAME'],
										'domain' => $_SERVER['SERVER_NAME'],
										'path'   => '/',
										'prefix' => 'grc_',
										);
							$this->input->set_cookie($cookie);
							$this->data['user_detail'] = $this->data['mail_status'];
							$this->session->set_userdata('study_name_data', $this->security->xss_clean($this->data['user_detail']));
							$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
							$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
							$study_data['chapter_title'] = $this->study_model->getChapterDetail($study_data);
							$question_detail = $this->study_model->get_questions_detail($study_data);
							redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($study_data['chapter_title'])))."&q_type=".trim($question_detail['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail['question_topic'])))."&question_id=".trim($question_detail['question_mcq_seq_id']));
						}
						else {
							$this->data['error'] = $this->lang->line('form_validation_duplicate_email');
							$this->session->set_userdata('study_change_email_data', $value);
							$this->data['post_value'] = $this->security->xss_clean($this->input->post());
							$this->load->view('study/study_info_collect', $this->data);
						}
						
					}
					else if(!$this->study_model->verify_UserVisitName($this->security->xss_clean($this->input->post()), $this->security->xss_clean($value[0]['member_id'])))
					{
						$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($value[0]['member_id']));
						$activation = $this->data['mail_status'][0]['activation'];
						$email = $this->data['mail_status'][0]['email']; 
						if(isset($this->data['mail_status'][0]['active']) && $this->data['mail_status'][0]['active']=='N')
						{
							$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
							$this->common_model->email_send($email, $mail_msg);
						}
						
						$this->study_model->updateUserVisitData(array('first_name'=>$fname, 'last_name'=>$lname), $this->security->xss_clean($value[0]['member_id']));
						// set cookie 
						$cookie = array(
									'name'   => '347786263',
									'value'  => base64_encode(htmlspecialchars($value[0]['member_id'], ENT_QUOTES)),
									'expire' => 86400 * 7,
									//'domain' => '.'.$_SERVER['SERVER_NAME'],
									'domain' => $_SERVER['SERVER_NAME'],
									'path'   => '/',
									'prefix' => 'grc_',
									);
						$this->input->set_cookie($cookie);
						$this->data['user_detail'] = $this->data['mail_status'];
						$this->session->set_userdata('study_name_data', $this->security->xss_clean($this->data['user_detail']));
						$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
						$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
						$study_data['chapter_title'] = $this->study_model->getChapterDetail($study_data);
						$question_detail = $this->study_model->get_questions_detail($study_data);
						redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($study_data['chapter_title'])))."&q_type=".trim($question_detail['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail['question_topic'])))."&question_id=".trim($question_detail['question_mcq_seq_id']));

					}
					else {
						$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($value[0]['member_id']));
						$activation = $this->data['mail_status'][0]['activation'];
						$email = $this->data['mail_status'][0]['email'];
						if(isset($this->data['mail_status'][0]['active']) && $this->data['mail_status'][0]['active']=='N')
						{
							$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
							$this->common_model->email_send($email, $mail_msg);
						}
						
						$this->data['user_detail'] = $this->common_model->getUserDataById($this->security->xss_clean($value[0]['member_id']));
						$this->session->set_userdata('study_name_data', $this->security->xss_clean($this->data['user_detail']));
						redirect(BASE_URL.'study/get_school');
					}
				}
				else {
					$this->data['error'] = validation_errors();
					$this->load->view('study/study_info_collect', $this->data);
				}
			}
			else if($this->input->post('category') == 'later')
			{
				$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
				$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
				$study_data['chapter_title'] = $this->study_model->getChapterDetail($study_data);
				$question_detail = $this->study_model->get_questions_detail($study_data);
				redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($study_data['chapter_title'])))."&q_type=".trim($question_detail['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail['question_topic'])))."&question_id=".trim($question_detail['question_mcq_seq_id']));
			}
		}
		//if email cookie not exists 
		else {
			if($this->session->userdata('study_email_data'))
			{
				$this->data['change_mail'] = $this->security->xss_clean($this->session->userdata('change_email'));
				$this->session->unset_userdata('change_email');
				$this->load->view('study/study_info_collect', $this->data);
			}
			else {
				redirect(BASE_URL.'study');
			}
		}
	}

	public function getSchool()
	{
		$this->load->helper('cookie');
		
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$this->data['page_type'] = 'school';
		$s_cookie = 'grc_724665034778';
		
		//if email cookie exists
		if($this->input->cookie($s_cookie, TRUE))
		{
			$c_school_id = base64_decode($this->security->xss_clean($this->input->cookie($s_cookie, TRUE)));

			$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($c_school_id));
			
			$this->data['user_detail'] = $this->data['mail_status'];
			$this->session->set_userdata('study_school_data', $this->security->xss_clean($this->data['user_detail']));
			redirect(BASE_URL.'study/get_section');
		}
		//if email entered
		else if($this->input->post('school_enter') == 'school')
		{
			$school = $this->security->xss_clean($this->input->post('school1'));
			
			if($this->input->post('category') == 'now')
			{
				$this->form_validation->set_rules('school1',$this->data['label']['school'],'trim|required|xss_clean|alpha_dash');
				
				if ($this->form_validation->run())
				{
					$value = $this->session->userdata('study_name_data');
					$this->session->unset_userdata('study_name_data');
					
					if(!$this->study_model->verify_UserVisitSchool($this->security->xss_clean($this->input->cookie('zrc_first_school', TRUE))))
					{
						$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($value[0]['member_id']));
						$activation = $this->data['mail_status'][0]['activation'];
						$email = $this->data['mail_status'][0]['email'];
						if(isset($this->data['mail_status'][0]['active']) && $this->data['mail_status'][0]['active']=='N')
						{
							$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
							$this->common_model->email_send($email, $mail_msg);
						}
						
						$this->study_model->updateUserVisitData($this->security->xss_clean($this->input->post()), $this->security->xss_clean($value[0]['member_id']),'school');
						// set cookie 
						$cookie = array(
								'name'   => '724665034778',
								'value'  => base64_encode(htmlspecialchars($this->security->xss_clean($value[0]['member_id']), ENT_QUOTES)),
								'expire' => 86400 * 7,
								//'domain' => '.'.$_SERVER['SERVER_NAME'],
								'domain' => $_SERVER['SERVER_NAME'],
								'path'   => '/',
								'prefix' => 'grc_',
								);
						 
						$this->input->set_cookie($cookie);
						$this->data['user_detail'] = $this->data['mail_status'];
						$this->session->set_userdata('study_school_data', $this->security->xss_clean($this->data['user_detail']));
						$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
						$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
						$study_data['chapter_title'] = $this->study_model->getChapterDetail($study_data);
						$question_detail = $this->study_model->get_questions_detail($study_data);
						redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($study_data['chapter_title'])))."&q_type=".trim($question_detail['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail['question_topic'])))."&question_id=".trim($question_detail['question_mcq_seq_id']));
					}
					else {
						$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($value[0]['member_id']));
						$activation = $this->data['mail_status'][0]['activation'];
						$email = $this->data['mail_status'][0]['email'];
						if(isset($this->data['mail_status'][0]['active']) && $this->data['mail_status'][0]['active']=='N')
						{
							$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
							$this->common_model->email_send($email, $mail_msg);
						}
						
						$this->data['user_detail'] = $this->common_model->getUserDataById($this->security->xss_clean($value[0]['member_id']));
						$this->session->set_userdata('study_school_data', $this->security->xss_clean($this->data['user_detail']));
						redirect(BASE_URL.'study/get_section');
					}
				}
				else {
					$this->data['error'] = validation_errors();
					$this->load->view('study/study_info_collect', $this->data);
				}
			}
			else if($this->input->post('category') == 'later')
			{
				$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
				$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
				$study_data['chapter_title'] = $this->study_model->getChapterDetail($study_data);
				$question_detail = $this->study_model->get_questions_detail($study_data);
				redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($study_data['chapter_title'])))."&q_type=".trim($question_detail['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail['question_topic'])))."&question_id=".trim($question_detail['question_mcq_seq_id']));
			}
		}
		//if email cookie not exists 
		else {
			if($this->session->userdata('study_name_data'))
			{
				$this->data['user_value'] = $this->security->xss_clean($this->session->userdata('study_name_data'));
				$this->load->view('study/study_info_collect', $this->data);
			}
			else {
				redirect(BASE_URL.'study');
			}
		}
	}

	public function getSection()
	{
		$this->load->helper('cookie');
		
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$this->data['page_type'] = 'section';
		$sec_cookie = 'grc_732347788466';
		
		//if email cookie exists
		if($this->input->cookie($sec_cookie, TRUE))
		{
			$c_section_id = base64_decode($this->security->xss_clean($this->input->cookie($sec_cookie, TRUE)));
			
			$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($c_section_id));
			
			$this->data['user_detail'] = $this->data['mail_status'];
			$this->session->set_userdata('study_school_data', $this->security->xss_clean($this->data['user_detail']));
			$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
			
			$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
			$study_data['chapter_title'] = $this->study_model->getChapterDetail($study_data);
			$question_detail = $this->study_model->get_questions_detail($study_data);
			redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($study_data['chapter_title'])))."&q_type=".trim($question_detail['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail['question_topic'])))."&question_id=".trim($question_detail['question_mcq_seq_id']));
		}
		//if email entered
		else if($this->input->post('section_enter') == 'section')
		{
			$section = $this->security->xss_clean($this->input->post('section1'));
			$this->form_validation->set_rules('section1',$this->data['label']['section'],'trim|required|xss_clean|alpha_dash');
				
				if ($this->form_validation->run())
				{
					$value = $this->session->userdata('study_school_data');
					$this->session->unset_userdata('study_school_data');

					if(!$this->study_model->verify_UserVisitSection($this->security->xss_clean($value), $section))
					{
						$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($value[0]['member_id']));
						$activation = $this->data['mail_status'][0]['activation'];
						$email = $this->data['mail_status'][0]['email'];
						if(isset($this->data['mail_status'][0]['active']) && $this->data['mail_status'][0]['active']=='N')
						{
							$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
							$this->common_model->email_send($email, $mail_msg);
						}
						
						$this->study_model->updateUserVisitData($this->security->xss_clean($this->input->post()), $this->security->xss_clean($value[0]['member_id']),'section');
						// set cookie 
						$cookie = array(
								'name'   => '732347788466',
								'value'  => base64_encode(htmlspecialchars($this->security->xss_clean($value[0]['member_id']), ENT_QUOTES)),
								'expire' => 86400 * 7,
								//'domain' => '.'.$_SERVER['SERVER_NAME'],
								'domain' => $_SERVER['SERVER_NAME'],
								'path'   => '/',
								'prefix' => 'grc_',
								);

						$this->input->set_cookie($cookie);
						$this->data['user_detail'] = $this->data['mail_status'];
						$this->session->set_userdata('study_school_data', $this->security->xss_clean($this->data['user_detail']));
						$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
						$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
						$study_data['chapter_title'] = $this->study_model->getChapterDetail($study_data);
						$question_detail = $this->study_model->get_questions_detail($study_data);
						redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($study_data['chapter_title'])))."&q_type=".trim($question_detail['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail['question_topic'])))."&question_id=".trim($question_detail['question_mcq_seq_id']));
					}
					else {
						$this->data['mail_status'] = $this->common_model->getUserDataById($this->security->xss_clean($value[0]['member_id']));
						$activation = $this->data['mail_status'][0]['activation'];
						$email = $this->data['mail_status'][0]['email'];
						if(isset($this->data['mail_status'][0]['active']) && $this->data['mail_status'][0]['active']=='N')
						{
							$mail_msg = 'Thanks for registering. Please generate your pass. <a href="'.base_url().'index.php/account_activate/'.$activation.'?ref=study">Generate Password</a>';
							$this->common_model->email_send($email, $mail_msg);
						}

						$this->data['user_detail'] = $this->data['mail_status'];
						$this->session->set_userdata('study_school_data', $this->security->xss_clean($this->data['user_detail']));
						$this->session->set_userdata('study_email_data', $this->security->xss_clean($this->input->post()));
						$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
						$question_detail = $this->study_model->getChapterDetail($study_data);
						redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($study_data['chapter_title'])))."&q_type=".trim($question_detail['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail['question_topic'])))."&question_id=".trim($question_detail['question_mcq_seq_id']));
					}
				}
				else {
					$this->data['error'] = validation_errors();
					$this->data['study_data'] = $this->security->xss_clean($this->session->userdata('study_data'));
					$this->load->view('study/study_info_collect', $this->data);
				}
		}
		//if email cookie not exists 
		else {
			if($this->session->userdata('study_school_data'))
			{
				$this->data['user_value'] = $this->security->xss_clean($this->session->userdata('study_school_data'));
				$this->data['study_data'] = $this->security->xss_clean($this->session->userdata('study_data'));
				$this->load->view('study/study_info_collect', $this->data);
			}
			else {
				redirect(BASE_URL.'study');
			}
		}
	}

public function redirectQtype($value='')
{
	if($value == '')
	{
		redirect(BASE_URL.'study');
	}
	else {
		$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
		$question_detail = $this->study_model->getChapterDetail($study_data, $value);
		//var_dump($question_detail);die();
		redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($question_detail[0]['chapter_title'])))."&q_type=".trim($value)."&q_topic=".urlencode(strtolower(trim($question_detail[0]['question_topic'])))."&question_id=".trim($question_detail[0]['question_mcq_seq_id']));
		exit();
	}
}	

################### Analytics, Que Share and Floater bar code starts here   #############################
	public function getBook()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		$this->data['member_id'] = $member_id;
		$this->data['input_data'] = $this->security->xss_clean($this->input->get());
		
		if(isset($member_id) && $member_id != '')
		{
			$this->set_question_access_counts();
		}
		
		$flag = FALSE;
		foreach($this->data['input_data'] as $temp)
		{
			if (preg_match('/^[A-Za-z0-9\,\. +]+$/', $temp))
			{
				$flag = TRUE;
			}
		}
		if($flag===FALSE)
		{
			redirect(BASE_URL.'study');
			exit;
		}
		else if(isset($this->data['input_data']['question_id']) && $this->data['input_data']['question_id']!='')
		{
			if($this->input->post('question_id'))
			{
				$question_id = $this->input->post('question_id');
			}
			else
			{
				$question_id = $this->input->get('question_id');
			}
			//var_dump($this->session->userdata('study_data'));die();
			//$this->data['questions_by_type'] = $this->study_model->filter_questions_by_type($this->data['input_data'],$this->session->userdata('study_data'), 'all');
			
			
			//=========== fetching question/answer and creating pagination =============//
			$this->data['q_count_all'] = $this->study_model->filter_questions_by_type($this->data['input_data'],$this->session->userdata('study_data'), 'all');
			
			//---setup for pagination ---//
			$adjacents = 2;
			$total_pages = count($this->data['q_count_all']);
			$targetpage = BASE_URL.'study/book?'.$_SERVER['QUERY_STRING'];
			$limit = 5; 								//how many items to show per page
			$page = @$_GET['page'];
			if($page) 
				$start = ($page - 1) * $limit; 			//first item to display on this page
			else
				$start = 0;								//if no page var is given, set start to 0
			
			if ($page == 0) $page = 1;					//if no page var is given, default to 1.
			$prev = $page - 1;							//previous page is page - 1
			$next = $page + 1;							//next page is page + 1
			$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
			$lpm1 = $lastpage - 1;						//last page minus 1
			
			$this->data['pagination'] = $this->createPagination($adjacents,$total_pages,$targetpage,$limit,$page,$start,$prev,$next,$lastpage,$lpm1);
			//echo $this->data['pagination'];die();
			//---setup for pagination ---//
			
			$this->data['questions_by_type'] = $this->study_model->filter_questions_by_type($this->data['input_data'],$this->session->userdata('study_data'),$start,$limit);
			
			$que = '';
			if(isset($this->data['input_data']['question_id']) && $this->data['input_data']['question_id']!='')
			{
				$que = $this->security->xss_clean($this->data['input_data']['question_id']);
			}
			else if(isset($this->data['questions_by_type'][0]['question_mcq_seq_id']) && $this->data['questions_by_type'][0]['question_mcq_seq_id'] != '')
			{
				$que = $this->security->xss_clean($this->data['questions_by_type'][0]['question_mcq_seq_id']);
			}
			$this->data['anwswers_by_type'] = $this->study_model->filter_answers_by_type($que);
			//=========== fetching question/answer and creating pagination =============//
			
			
			//if($this->data['questions_by_type'] != 'no result')
			//{
				//$this->data['next4_filtered_questions'] = $this->study_model->show_next4_filtered_questions($this->data['input_data'],$this->session->userdata('study_data'), 'all');
				//$this->data['q_count_all'] = $this->study_model->show_next4_filtered_questions($this->data['input_data'],$this->session->userdata('study_data'),'all');
			//}
			if($question_id)
			{
				if($this->session->userdata('id'))
				{
					$this->data['questions_like_status'] = $this->study_model->set_question_liked_status($question_id);
					$this->data['questions_note'] = $this->study_model->set_question_note($question_id);
					$this->data['questions_rate'] = $this->study_model->get_question_rate($question_id);
					$this->data['questions_flag'] = $this->study_model->get_question_flag($question_id);
				}
				else {
					$this->data['questions_like_status'] = '';
					$this->data['questions_note'] = '';
					$this->data['questions_rate'] = '';
					$this->data['questions_flag'] = '';
				}
			}
			else
			{
				$study_data = $this->security->xss_clean($this->session->userdata('study_data'));
				$question_detail = $this->study_model->getChapterDetail($study_data);
				redirect(BASE_URL."study/book?class=".trim($study_data['class'])."&board=cbse&subject=".urlencode(strtolower(trim($study_data['subject_full'])))."&book=".urlencode(strtolower(trim($study_data['book_full'])))."&chapter=".urlencode(strtolower(trim($study_data['chapter_title'])))."&q_type=".trim($question_detail['question_type'])."&q_topic=".urlencode(strtolower(trim($question_detail['question_topic'])))."&question_id=".trim($question_detail['question_mcq_seq_id']));
				exit();
			}
			
			$this->load->view('study/demo', $this->data);
		}
		else
		{
			//$this->data['questions_by_type'] = 'no result';
			$this->load->view('study/demo', $this->data);
		}
	}
	
	
	
	public function show_member_analytics()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];

		$this->data['question_type_count_data'] = $this->get_question_type_counts();		
		$this->data['questions_by_type'] = $this->filter_questions_by_type();		
		//$this->data['appearance_graph'] = $this->get_question_appearance_graph();	
			
		$this->load->view('study/analytics_script', $this->data);
	}
	
	public function get_question_access_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_access_counts($question_id);
			echo $data['model_data'];			
		}
	}
	
	public function get_question_access_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_access_counts_graph_weekly($question_id);
			$this->data['access_count_data_weekly'] = $data['model_data'];
			$this->data['section'] = 'access_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);

		}
	}
	
	public function get_question_access_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_access_counts_graph_monthly($question_id);
			$this->data['access_count_data_monthly'] = $data['model_data'];
			$this->data['section'] = 'access_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}
	
	public function set_question_access_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->security->xss_clean($this->input->get('question_id'));
			$type = 'study';
			$this->data['data'] = array(
				'type' => 'study',
			);
			$data['model_data'] = $this->study_model->set_question_access_counts($question_id, $this->data['data']);
		}
	}

	public function get_question_download_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_download_counts($question_id);
			echo $data['model_data'];
		}
	}
	
	
	public function get_question_download_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_download_counts_graph_weekly($question_id);
			$this->data['download_count_data_weekly'] = $data['model_data'];
			$this->data['section'] = 'downloads_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}

	public function get_question_download_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_download_counts_graph_monthly($question_id);
			$this->data['download_count_data_monthly'] = $data['model_data'];
			//return $this->data['download_count_data_monthly'];
			$this->data['section'] = 'downloads_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}
	
	public function get_question_liked_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			echo $data['model_data'] = $this->study_model->get_question_liked_counts($question_id);
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	
	public function get_question_liked_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_liked_counts_graph_weekly($question_id);
			$this->data['liked_count_data_weekly'] = $data['model_data'];
			//return $this->data['liked_count_data'];
			$this->data['section'] = 'likes_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}

	public function get_question_liked_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_liked_counts_graph_monthly($question_id);
			$this->data['liked_count_data_monthly'] = $data['model_data'];
			//return $this->data['liked_count_data_monthly'];
			$this->data['section'] = 'likes_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}

	public function set_question_liked_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$this->data['data'] = "";
			$data['model_data'] = $this->study_model->set_question_liked_counts($question_id, $this->data['data']);
			//print_r($data['model_data']);
		}
	}
	
	public function get_question_shared_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');

		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_shared_counts($question_id);
			foreach($data['model_data'] as $row) {
				print_r($row);
				echo "<br />";
			}
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	public function get_question_shared_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_shared_counts_graph_weekly($question_id);
			//var_dump($data['model_data']); die();
			$this->data['shared_count_data_weekly'] = $data['model_data'];
			//return $this->data['shared_count_data'];
			$this->data['section'] = 'shares_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}
	
	public function get_question_shared_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_shared_counts_graph_monthly($question_id);
			//var_dump($data['model_data']); die();
			$this->data['shared_count_data_monthly'] = $data['model_data'];
			//return $this->data['shared_count_data_monthly'];
			$this->data['section'] = 'shares_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
/*	public function set_question_shared_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		//$member_id = $this->session->userdata('id');
		$member_id = 1;
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
	//		if($this->input->post())
	//		{			
				$this->data['data'] = array(
					'shared_platform' => 'twitter'
				);
				//print_r($this->data['data']);
				//$this->data['data'] = $this->input->post();
				echo $data['model_data'] = $this->study_model->set_question_shared_counts($question_id, $this->data['data']);
				//$this->load->view('study/analytics_script', $this->data);
	//		}
		}
	}
*/	
	
	public function get_question_flag_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_flag_counts($question_id);
			foreach($data['model_data'] as $row) {
				print_r($row);
				echo "<br />";
			}
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	public function get_question_flag_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_flag_counts_graph_weekly($question_id);
			//var_dump($data['model_data']); die();
			$this->data['flag_count_data_weekly'] = $data['model_data'];
			//return $this->data['flag_count_data'];
			$this->data['section'] = 'flags_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}
	
	public function get_question_flag_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_flag_counts_graph_monthly($question_id);
			//var_dump($data['model_data']); die();
			$this->data['flag_count_data_monthly'] = $data['model_data'];
			//return $this->data['flag_count_data_monthly'];
			$this->data['section'] = 'flags_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}
	
	public function set_question_flags()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->security->xss_clean($this->input->post('question_id'));
			$flag_type = $this->security->xss_clean($this->input->post('flag_type'));
			$this->data['data'] = array(
				'flag_type' => $flag_type,
				'flag_description' => 'This question is incorrect, please correct this asap'
				);
				echo $data['model_data'] = $this->study_model->set_question_flags($question_id, $this->data['data']);
				//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	public function get_question_time_to_answer()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_time_to_answer($question_id);
			echo $data['model_data'];
			//$this->load->view('study/analytics_script', $this->data);
		}
	}


	public function get_question_time_to_answer_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_time_to_answer_graph_weekly($question_id);
			$this->data['answer_time_data_weekly'] = $data['model_data'];
			//return $this->data['answer_time_data'];
			$this->data['section'] = 'answertime_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}


	public function get_question_time_to_answer_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_time_to_answer_graph_monthly($question_id);
			$this->data['answer_time_data_monthly'] = $data['model_data'];
			//return $this->data['answer_time_data_monthly'];
			$this->data['section'] = 'answertime_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);
		}
	}

	public function set_question_time_to_answer()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
	//		if($this->input->post())
	//		{			
				$this->data['data'] = array(
					'correct_answer' => '1',
					'correct_answer_time' => '8.10',
					'wrong_answer' => '0',
					'wrong_answer_time' => '',
					'answer_time' => '10.00'
				);
				//print_r($this->data['data']);
				//$this->data['data'] = $this->input->post();
				$data['model_data'] = $this->study_model->set_question_time_to_answer($question_id, $this->data['data']);
				print_r($data['model_data']);
				//$this->load->view('study/analytics_script', $this->data);
	//		}
		}
	}
	
	public function get_question_appearance()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data']=$this->study_model->get_question_appearance($question_id);
			foreach($data['model_data'] as $row) {
				print_r($row);
				echo "<br />";
			}
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

/*
	public function get_question_appearance_graph()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$member_id = $this->session->userdata('id');
		$member_id = 1;
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_appearance_graph($question_id);
			$this->data['appearance_data'] = $data['model_data'];
			return $this->data['appearance_data'];

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
*/
	
	public function get_question_correct_answer_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_correct_answer_counts($question_id);
			echo $data['model_data'];
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	public function get_question_correct_answer_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_correct_answer_counts_graph_weekly($question_id);
			$this->data['correct_answer_counts_graph_weekly'] = $data['model_data'];
			//return $this->data['correct_answer_count_data'];
			$this->data['section'] = 'correctanswer_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	public function get_question_correct_answer_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_correct_answer_counts_graph_monthly($question_id);
			$this->data['correct_answer_counts_graph_monthly'] = $data['model_data'];
			//return $this->data['correct_answer_count_data_monthly'];
			$this->data['section'] = 'correctanswer_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	public function get_question_wrong_answer_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_wrong_answer_counts($question_id);
			echo $data['model_data'];
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	public function get_question_wrong_answer_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_wrong_answer_counts_graph_weekly($question_id);
			$this->data['wrong_answer_counts_graph_weekly'] = $data['model_data'];
			//return $this->data['wrong_answer_count_data_weekly'];
			$this->data['section'] = 'wronganswer_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	public function get_question_wrong_answer_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_question_wrong_answer_counts_graph_monthly($question_id);
			$this->data['wrong_answer_counts_graph_monthly'] = $data['model_data'];
			//return $this->data['wrong_answer_count_data_monthly'];
			$this->data['section'] = 'wronganswer_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	public function get_question_type_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$questiontype_id = $this->input->post('question_id');
			$this->data['question_type_count_data'] = $this->study_model->get_question_type_counts($questiontype_id);
			return $this->data['question_type_count_data'];
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	public function filter_questions_by_type()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$questiontype_id = $this->input->post('question_id');
			$this->data['questions_by_type'] = $this->study_model->filter_questions_by_type($questiontype_id);
			return $this->data['questions_by_type'];
			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	
	#################### Flexi Pad Starts here ####################
	

	public function show_events()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$this->data['data_model'] = $this->study_model->show_events();
		//print_r($this->data['data_model']);
		$this->load->view('study/events', $this->data);
	}

	public function edit_event_form()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$event_id = $this->input->get('event_id');
		$this->data['event_data'] = $this->study_model->edit_event($event_id);
		$this->load->view('study/edit_event_form', $this->data);
	}

	public function add_event()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		if($this->input->post())
		{	
			$this->form_validation->set_rules('eventname', 'Event Name', 'trim|required');
			$this->form_validation->set_rules('eventdesc', 'Description', 'trim|required');
			$this->form_validation->set_rules('eventdate', 'Event Date', 'trim|required');
			$this->form_validation->set_rules('eventtime', 'Event Time', 'trim|required');
//			$this->form_validation->set_rules('remindb4', 'Remind Before', 'trim|required');
			$this->form_validation->set_rules('eventplace', 'Event Place', 'trim|required');
			
			// Run the validation.
				if ($this->form_validation->run())
				{
					//print_r($this->input->post());
					$this->data['input_data'] = $this->input->post();
					$date = date('Y-m-d H:i:s');
					echo $this->study_model->add_event($this->data['input_data']);
					redirect(base_url().BASE.'study/show_events');
//					$this->load->view('study/events', $this->data);
				}
				else
				{
					$this->data['input_data'] = $this->input->post();
					$this->data['error'] = validation_errors('<span>', '</span><br />');
					$this->load->view('study/add_event_form', $this->data);
				}
		}
	}
	
	public function update_event()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		if($this->input->post())
		{	
			$this->form_validation->set_rules('eventname', 'Event Name', 'trim|required');
			$this->form_validation->set_rules('eventdesc', 'Description', 'trim|required');
			$this->form_validation->set_rules('eventdate', 'Event Date', 'trim|required');
			$this->form_validation->set_rules('eventtime', 'Event Time', 'trim|required');
//			$this->form_validation->set_rules('remindb4', 'Remind Before', 'trim|required');
			$this->form_validation->set_rules('eventplace', 'Event Place', 'trim|required');
			
			// Run the validation.
				if ($this->form_validation->run())
				{
					//print_r($this->input->post());
					$this->data['input_data'] = $this->input->post();
					$date = date('Y-m-d H:i:s');
					echo $this->study_model->update_event($this->data['input_data']);
					redirect(base_url().BASE.'study/show_events');
					//$this->load->view('study/show_events', $this->data);
				}
				else
				{
					$this->data['input_data'] = $this->input->post();
					$this->data['error'] = validation_errors('<span>', '</span><br />');
					$this->load->view('study/add_event_form', $this->data);
				}
		}
	}
	
	
	
	#################### Flexi Pad Ends here ######################

	
	#################### Jagdish's demo controller code starts here ######################
	
		//public function share(){
//		$this->load->view('demo/share');
//	}
	
	public function share_entry(){
		$share = $this->input->post();
		//$share = 'facebook';
		if($this->study_model->sharing_entry($share)){
			echo 'pass';
		}
		else {
			echo 'fail';
		}
	}
	
	public function create_event(){
		if($this->input->post()){
//			print_r($this->input->post('eventname'));
//			die;
			//var_dump($this->input->post()); die();
			$this->load->library('form_validation');
			$this->form_validation->set_rules('eventname', 'Event Name', 'trim|required');
			$this->form_validation->set_rules('eventdesc', 'Description', 'trim|required');
			$this->form_validation->set_rules('eventdate', 'Event Date', 'trim|required');
			$this->form_validation->set_rules('eventtime', 'Event Time', 'trim|required');
//			$this->form_validation->set_rules('remindb4', 'Remind Before', 'trim|required');
			$this->form_validation->set_rules('eventplace', 'Event Place', 'trim|required');

			//print_r($form);
			if($this->form_validation->run()) {
				$form=array();
				$form =$this->input->post();
					print_r($this->study_model->create_event_model($form));
			}
			
		}
		//$this->load->view('demo/event');
	}
	
	public function chart(){
		$data['chart'] = $this->study_model->chart_data();
		$this->load->view('demo/question_analytics/chart',$data);
	}
	
	public function question_analytics(){
		$value = $this->input->post('value');
		$data['data_all'] = $this->study_model->question_analytics($value);
		//var_dump($data['data_all'][0]['question_access_count']); die();
		//echo "hello";
		if($value=='access'){
			echo $data['data_all'][0]['question_access_count'];
		}
		if($value=='shared'){
			echo $data['data_all'][0]['question_shared_count'];
		}
		if($value=='like'){
			echo $data['data_all'][0]['question_liked_count'];
		}
		if($value=='download'){
			echo $data['data_all'][0]['question_downloaded_count'];
		}
		if($value=='dislike'){
			echo $data['data_all'][0]['question_flag_count'];
		}
		if($value=='correct'){
			echo $data['data_all'][0]['question_correct_answer_count'];
		}
		if($value=='incorrect'){
			echo $data['data_all'][0]['question_wrong_answer_count'];
		}
	}
	
	public function upload_questions(){
		if($this->input->post('questions')){
			$this->load->library('form_validation');
			$this->form_validation->set_rules('questions','Questions','required|trim');
			$this->form_validation->set_rules('answers','Answers','required|trim');
			if($this->form_validation->run()) {
				$form = array();
				$form = $this->input->post();
				echo $this->study_model->member_question_upload_model($form);	
			}
		}
		//$this->load->view('demo/upload_question');
	}
	

	/*public function add_question_tip()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		if($this->input->post())
		{	
			$this->form_validation->set_rules('question_tip', 'Question Tip', 'trim|required');
			
			// Run the validation.
				if ($this->form_validation->run())
				{
					$this->data['input_data'] = $this->input->post();
					$date = date('Y-m-d H:i:s');
					$que_id = $this->input->post('question_id');
					//$que_tip = "Try this question as much as possible on paper";
					echo $this->study_model->add_question_tip($this->data['input_data'],$que_id);
//					redirect(base_url().BASE.'study/show_events');
//					$this->load->view('study/events', $this->data);
				}
/*				else
				{
					$this->data['input_data'] = $this->input->post();
					$this->data['error'] = validation_errors('<span>', '</span><br />');
				}
*/		/*}
	}*/
	
	
	public function feedback(){
		if($this->session->userdata('logedin')){
			$this->session->set_userdata('feedback_user_insert_id',$this->session->userdata('id'));
			$this->feedback1();	
		}
		else {
		
			if($this->input->post('category')){
				$this->form_validation->set_rules('category','category','required|trim');
				if($this->form_validation->run()) {
					$this->session->set_userdata('feedback_category',$this->input->post('category'));
					if($this->session->userdata('feedback_category')){
						redirect(BASE_URL.'study/feedback_userdetails');
						exit();
					}
				}
			}else {
				$this->load->view('demo/feedback/feedback',$this->data);
			}
		}
	}
	
	public function feedback_userdetails(){
		
		if($this->input->post('name')){
			$this->form_validation->set_rules('name','Name','required|trim');
			$this->form_validation->set_rules('email','Email','required|trim');
			if($this->form_validation->run()) {
					$form = array();
					$form = $this->input->post();
					
					if($this->study_model->user_email_check_feedback()){
						
						redirect(BASE_URL.'study/feedback1');
						exit();
					}
					else {		
								
						if($this->study_model->register_user_feedback($form)){
							redirect(BASE_URL.'study/feedback1');
							exit();
						}
					}
			}
		}
		$this->load->view('demo/feedback/feedback_userdetails',$this->data);
	}
	public function feedback1(){
		$this->load->view('demo/feedback/feedback1',$this->data);
	}
	
	public function feedback_appreciate(){
		if($this->session->userdata('feedback_user_insert_id')){
			if($this->input->post('appreciate_feedback')){
				$this->form_validation->set_rules('appreciate_feedback','Feedback','required|trim');
				if($this->form_validation->run()) {
					if($this->study_model->insert_feedback($this->input->post('appreciate_feedback'),'appreciate')){
						$this->session->unset_userdata('feedback_user_insert_id');
						redirect(BASE_URL.'study/feedback');
						exit();
					}
				}
			}
			$this->load->view('demo/feedback/feedback_appreciate',$this->data);
		}else {
			redirect(BASE_URL.'study/feedback');
			exit();
		}
	}
	
	public function feedback_know_more(){
		if($this->session->userdata('feedback_user_insert_id')){
			if($this->input->post('knowmore_feedback')){
				$this->form_validation->set_rules('knowmore_feedback','Feedback','required|trim');
				if($this->form_validation->run()) {
					if($this->study_model->insert_feedback($this->input->post('knowmore_feedback'),'know more')){
						$this->session->unset_userdata('feedback_user_insert_id');
						redirect(BASE_URL.'study/study_demo');
						exit();
					}
				}
			}
			$this->load->view('demo/feedback/feedback_know_more',$this->data);
		}
		else {
			redirect(BASE_URL.'study/study_demo');
			exit();
		}
		
	}
	public function feedback_comment(){
		if($this->session->userdata('feedback_user_insert_id')){
			if($this->input->post('comment_feedback')){
				$this->form_validation->set_rules('comment_feedback','Feedback','required|trim');
				if($this->form_validation->run()) {
					if($this->study_model->insert_feedback($this->input->post('comment_feedback'),'comment')){
						$this->session->unset_userdata('feedback_user_insert_id');
						redirect(BASE_URL.'study/study_demo');
						exit();
					}
				}
			}
			$this->load->view('demo/feedback/feedback_comment',$this->data);
		}
		else {
			redirect(BASE_URL.'study/study_demo');
			exit();
		}
	}
	
	public function feedback_dislike(){
		if($this->session->userdata('feedback_user_insert_id')){
			if($this->input->post('dislike_feedback')){
				$this->form_validation->set_rules('dislike_feedback','Feedback','required|trim');
				if($this->form_validation->run()) {
					if($this->study_model->insert_feedback($this->input->post('dislike_feedback'),'dislike')){
						$this->session->unset_userdata('feedback_user_insert_id');
						redirect(BASE_URL.'study/study_demo');
						exit();
					}
				}
			}
			$this->load->view('demo/feedback/feedback_dislike',$this->data);
		}
		else {
			redirect(BASE_URL.'study/study_demo');
			exit();
		}
	}
	
	public function feedback_employment(){
		if($this->session->userdata('feedback_user_insert_id')){
			if($this->input->post('feedback_employment')){
				$this->form_validation->set_rules('feedback_employment','Feedback','required|trim');
				if($this->form_validation->run()) {
					if($this->study_model->insert_feedback($this->input->post('feedback_employment'),'employment')){
						$this->session->unset_userdata('feedback_user_insert_id');
						redirect(BASE_URL.'study/study_demo');
						exit();
					}
				}
			}
			$this->load->view('demo/feedback/feedback_employment',$this->data);
		}
		else {
			redirect(BASE_URL.'study/study_demo');
			exit();
		}
	}

	
	
	#################### Jagdish's demo controller code ends here ######################
	
	
	public function set_question_note_or_bookmark()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->security->xss_clean($this->input->post('question_id'));
			$this->data['data'] = $this->security->xss_clean($this->input->post());
			$data['model_data'] = $this->study_model->set_question_note_or_bookmark($question_id, $this->data['data']);
			echo $data['model_data'];
		}		
	}
	
	
	public function question_print()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
	//		if($this->input->post())
	//		{			
				$this->data['data'] = "";
				//print_r($this->data['data']);
				//$this->data['data'] = $this->input->post();
				$data['model_data'] = $this->study_model->question_print($question_id, $this->data['data']);
				echo $data['model_data'];
	//		}
		}		
	}
	
	public function rate_this_question()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->security->xss_clean($this->input->post('question_id'));
			$this->data['data'] = $this->input->post();
			$data['model_data'] = $this->study_model->rate_this_question($question_id, $this->data['data']);
			echo $data['model_data'];
		}		
	}
	
	

	#################### HTML To PDF Converter starts here ######################

    public function download() {   
	
        // Load all views as normal
/*		$sql_select = "question_description, question_answer_description";
		$this->db->select($sql_select);
		$this->db->where('question_mcq_seq_id','BIEN11006029');
		$query= $this->db->get($this->db->dbprefix('reviewed_questions'));
	    $result = $query->result_array();
		$myfile = fopen($_SERVER['DOCUMENT_ROOT']."/downloads/newfile.html", "w") or die("Unable to open file!");
		$text = "Question:<br />".$result[0]['question_description']."<br /><br />";
		$text .= "Answer:<br />".$result[0]['question_answer_description'];
		echo $text;
		fwrite($myfile, $text);
		//fclose($myfile);*/
		
		$question_id = $this->security->xss_clean($this->input->get('question_id'));

		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$this->data['data'] = "";
			$data['model_data'] = $this->study_model->set_question_download_counts($question_id, $this->data['data']);
			if($data['model_data'] == "Success")
			{
				$this->load->view('study/welcome_message');
				// Get output html
				$html = $this->output->get_output();
				 
				// Load library
				$this->load->library('dompdf_gen');
				 
				// Convert to PDF
				$this->dompdf->load_html($html);
				$this->dompdf->render();
			
			//  $this->dompdf->stream("welcome.pdf",array('Attachment'=>0));
			
				//or without preview in browser
				$this->dompdf->stream("welcome.pdf");			
			}
			else
			{
				echo "<script type='text/javascript'>window.alert('".$data['model_data']."')</script>";
				echo "<script type='text/javascript'>window.close()</script>";
			}	
		}
		
    }

	#################### HTML To PDF Converter ends here ########################


	public function get_questionanalytics_access_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_access_counts($question_id);
			echo $data['model_data'];			
			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	public function get_questionanalytics_access_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_access_counts_graph_weekly($question_id);
			$this->data['access_count_data_weekly'] = $data['model_data'];
			$this->data['section'] = 'access_count';
			$this->data['period'] = $_GET['period'];
			//return $this->data['access_count_data_weekly'];
			$this->load->view('study/charts_q_analytics', $this->data);

		}
	}
	
	public function get_questionanalytics_access_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_access_counts_graph_monthly($question_id);
			$this->data['access_count_data_monthly'] = $data['model_data'];
			$this->data['section'] = 'access_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}
	
	public function get_questionanalytics_download_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_download_counts($question_id);
			echo $data['model_data'];
			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	
	public function get_questionanalytics_download_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_download_counts_graph_weekly($question_id);
			$this->data['download_count_data_weekly'] = $data['model_data'];
			//return $this->data['download_count_data'];
			$this->data['section'] = 'downloads_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}

	public function get_questionanalytics_download_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_download_counts_graph_monthly($question_id);
			$this->data['download_count_data_monthly'] = $data['model_data'];
			//return $this->data['download_count_data_monthly'];
			$this->data['section'] = 'downloads_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}

	
	public function get_questionanalytics_liked_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			echo $data['model_data'] = $this->study_model->get_questionanalytics_liked_counts($question_id);
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	
	public function get_questionanalytics_liked_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_liked_counts_graph_weekly($question_id);
			$this->data['liked_count_data_weekly'] = $data['model_data'];
			//return $this->data['liked_count_data'];
//			print_r($this->data['liked_count_data_weekly']);
			$this->data['section'] = 'likes_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}

	public function get_questionanalytics_liked_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_liked_counts_graph_monthly($question_id);
			$this->data['liked_count_data_monthly'] = $data['model_data'];
			//return $this->data['liked_count_data_monthly'];
			$this->data['section'] = 'likes_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}

	public function get_questionanalytics_shared_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_shared_counts($question_id);
			foreach($data['model_data'] as $row) {
				print_r($row);
				echo "<br />";
			}
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	public function get_questionanalytics_shared_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_shared_counts_graph_weekly($question_id);
			//var_dump($data['model_data']); die();
			$this->data['shared_count_data_weekly'] = $data['model_data'];
			//return $this->data['shared_count_data'];
			$this->data['section'] = 'shares_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}
	
	public function get_questionanalytics_shared_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_shared_counts_graph_monthly($question_id);
			//var_dump($data['model_data']); die();
			$this->data['shared_count_data_monthly'] = $data['model_data'];
			//return $this->data['shared_count_data_monthly'];
			$this->data['section'] = 'shares_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	
	public function get_questionanalytics_flag_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_flag_counts($question_id);
			foreach($data['model_data'] as $row) {
				print_r($row);
				echo "<br />";
			}
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	public function get_questionanalytics_flag_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_flag_counts_graph_weekly($question_id);
			//var_dump($data['model_data']); die();
			$this->data['flag_count_data_weekly'] = $data['model_data'];
			//return $this->data['flag_count_data'];
			$this->data['section'] = 'flags_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}
	
	public function get_questionanalytics_flag_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_flag_counts_graph_monthly($question_id);
			//var_dump($data['model_data']); die();
			$this->data['flag_count_data_monthly'] = $data['model_data'];
			//return $this->data['flag_count_data_monthly'];
			$this->data['section'] = 'flags_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}
	
	
	public function get_questionanalytics_time_to_answer()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_time_to_answer($question_id);
			echo $data['model_data'];
			//$this->load->view('study/analytics_script', $this->data);
		}
	}


	public function get_questionanalytics_time_to_answer_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_time_to_answer_graph_weekly($question_id);
			$this->data['answer_time_data_weekly'] = $data['model_data'];
			//return $this->data['answer_time_data'];
			$this->data['section'] = 'answertime_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}


	public function get_questionanalytics_time_to_answer_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_time_to_answer_graph_monthly($question_id);
			$this->data['answer_time_data_monthly'] = $data['model_data'];
			//return $this->data['answer_time_data_monthly'];
			$this->data['section'] = 'answertime_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);
		}
	}

	
	public function get_questionanalytics_correct_answer_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_correct_answer_counts($question_id);
			echo $data['model_data'];
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	public function get_questionanalytics_correct_answer_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_correct_answer_counts_graph_weekly($question_id);
			$this->data['correct_answer_counts_graph_weekly'] = $data['model_data'];
			//return $this->data['correct_answer_count_data'];
			$this->data['section'] = 'correctanswer_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	public function get_questionanalytics_correct_answer_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_correct_answer_counts_graph_monthly($question_id);
			$this->data['correct_answer_counts_graph_monthly'] = $data['model_data'];
			//return $this->data['correct_answer_count_data_monthly'];
			$this->data['section'] = 'correctanswer_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	public function get_questionanalytics_wrong_answer_counts()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		//$question_id = 1;
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_wrong_answer_counts($question_id);
			echo $data['model_data'];
			//$this->load->view('study/analytics_script', $this->data);
		}
	}

	public function get_questionanalytics_wrong_answer_counts_graph_weekly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_wrong_answer_counts_graph_weekly($question_id);
			$this->data['wrong_answer_counts_graph_weekly'] = $data['model_data'];
			//return $this->data['wrong_answer_count_data_weekly'];
			$this->data['section'] = 'wronganswer_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
	public function get_questionanalytics_wrong_answer_counts_graph_monthly()
	{
		$this->data['title'] =$this->title.$this->data['page']['study']['title'];
		$member_id = $this->session->userdata('id');
		
		if($member_id == "" || $member_id == NULL)
		{
			echo 'false';
		}
		else
		{
			$question_id = $this->input->post('question_id');
			$data['model_data'] = $this->study_model->get_questionanalytics_wrong_answer_counts_graph_monthly($question_id);
			$this->data['wrong_answer_counts_graph_monthly'] = $data['model_data'];
			$this->data['section'] = 'wronganswer_count';
			$this->data['period'] = $_GET['period'];
			$this->load->view('study/charts_q_analytics', $this->data);

			//$this->load->view('study/analytics_script', $this->data);
		}
	}
	
################### Analytics, Que Share and Floater bar code ends here   #############################

################### Function to create pagination   #############################
	public function createPagination($adjacents=NULL,$total_pages=NULL,$targetpage=NULL,$limit=NULL,$page=NULL,$start=NULL,$prev=NULL,$next=NULL,$lastpage=NULL,$lpm1=NULL)
	{
		$pagination = "";
		if($lastpage > 1)
		{	
			$pagination .= "<div id=\"page_navigation\">";
			//previous button
			if ($page > 1) 
				$pagination.= "<a href=\"$targetpage&page=$prev\"><<</a>";
			else
				$pagination.= "<span class=\"disabled\"><<</span>";	
			
			//pages	
			if ($lastpage < 5 + ($adjacents * 2))	//not enough pages to bother breaking it up
			{	
				for ($counter = 1; $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
				}
			}
			elseif($lastpage > 3 + ($adjacents * 2))	//enough pages to hide some
			{
				//close to beginning; only hide later pages
				if($page < 1 + ($adjacents * 2))		
				{
					for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
					{
						if ($counter == $page)
							$pagination.= "<span class=\"current\">$counter</span>";
						else
							$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
					}
					$pagination.= "...";
					$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
					$pagination.= "<a href=\"$targetpage&page=$lastpage\">$lastpage</a>";		
				}
				//in middle; hide some front and some back
				elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
				{
					$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
					$pagination.= "<a href=\"$targetpage&page=2\">2</a>";
					$pagination.= "...";
					for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
					{
						if ($counter == $page)
							$pagination.= "<span class=\"current\">$counter</span>";
						else
							$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
					}
					$pagination.= "...";
					$pagination.= "<a href=\"$targetpage&page=$lpm1\">$lpm1</a>";
					$pagination.= "<a href=\"$targetpage&page=$lastpage\">$lastpage</a>";		
				}
				//close to end; only hide early pages
				else
				{
					$pagination.= "<a href=\"$targetpage&page=1\">1</a>";
					$pagination.= "<a href=\"$targetpage&page=2\">2</a>";
					$pagination.= "...";
					for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
					{
						if ($counter == $page)
							$pagination.= "<span class=\"current\">$counter</span>";
						else
							$pagination.= "<a href=\"$targetpage&page=$counter\">$counter</a>";					
					}
				}
			}
			
			//next button
			if ($page < $counter - 1) 
				$pagination.= "<a href=\"$targetpage&page=$next\">>></a>";
			else
				$pagination.= "<span class=\"disabled\">>></span>";
			$pagination.= "</div>\n";		
		}
		return $pagination;
	}
################### Function to create pagination ends   #############################
}
?>